# coding:utf-8
import json

from django.db.models.query import QuerySet
from django.http import HttpResponseRedirect,HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

from teacher.models import stuchengji
from user.models import user
from functions import  *
from models import  jqcalendar

def chengji(request):
    xuehao=(request.user.user.id_card)
    chengjilist=stuchengji.objects.filter(num=xuehao).query
    chengjilist.group_by=['kecheng']
    chengjilist = QuerySet(query=chengjilist, model=stuchengji)
    list=[]
    for chengji in chengjilist:
        list.append(chengji)
    return render(request, "student/chengji.html", {'list':list})

def date(request):
        return render(request, "student/date.html", {})

def edit(request):
    eve={}
    t=request.GET.get('_',"")
    endtime=request.GET.get('end',"")
    id=request.GET.get('id',"")
    isallday=request.GET.get('isallday')
    start=request.GET.get('start',"")
    title=request.GET.get('title',"")
    if t != "":
        t=t[0:10]
        t=int(t)
        x = time.localtime(t)
        eve['sd']=time.strftime('%Y-%m-%d',x)
        eve['st']=time.strftime('%H:%M',x)
    if isallday:
        eve['isallday']=1
    else:
        eve['isallday']=0

    return render(request, "student/edit.html", eve)


# header('Content-type:text/javascript;charset=UTF-8');
@csrf_exempt
def datafeed(request):
    # events=jqcalendar.objects.all()
    method=request.GET.get('method')
    ret={}
    if method=="add":
        ret=addCalendar(request.POST.get("CalendarStartTime"),request.POST.get("CalendarEndTime"),request.POST.get("CalendarTitle"),request.POST.get("IsAllDayEvent"))
    elif method=="list":
        ret=listCalendar(request.POST.get("showdate"),request.POST.get("viewtype"))
        a=ret
        c=a
    elif method=="update":
        ret= updateCalendar(request.POST.get("calendarId"),request.POST.get("CalendarStartTime"),request.POST.get("CalendarEndTime"))
    elif method=="remove":
        ret=removeCalendar(request.POST.get("calendarId"))
    elif method=="adddetails":
        id = request.GET.get("id")
        st = request.POST.get("stpartdate") + " " +request.POST.get("stparttime")
        et =request.POST.get("etpartdate") +" " +request.POST.get("etparttime")
        if(id):
            ret = updateDetailedCalendar(id, st, et,
                request.POST.get("Subject"), IsAllDayEvent(),
                request.POST.get("Description"),
                request.POST.get("Location"), request.POST.get("colorvalue"), request.POST.get("timezone"))

        else:
            ret = addDetailedCalendar(st, et,
                request.POST.get("Subject"), IsAllDayEvent(), request.POST.get("Description"),
                request.POST.get("Location"), request.POST.get("colorvalue"), request.POST.get("timezone"))

    ret = json.dumps(ret)
    return HttpResponse(ret, content_type='application/json')


def hudong(request):
        return render(request, "student/hudong.html", {})

def  ziliao(request):
     return render(request, "student/ziliao.html", {})